<?php
namespace app\admin\controller;

use app\common\model\Config as ConfigModel;
/**
 * 后台配置控制器
 */
class Config extends Admin 
{
	/**
     * 配置管理
     */
    public function index(){
        $group = $this->request->param('group');
        $title = $this->request->param('title');
        //查询条件
        $where = [];
        $map = [];
        $query = [];
        if(isset($group)){
            $map['group'] = $group;
            $query['group'] = $group;
        }
        if(isset($title)){
            $where[] = ['title','like','%'.$title.'%'];
            $query['title'] = $title;
        }
        $list = ConfigModel::where($map)->where($where)->order('sort,id')->paginate(10, false, ['query' => $query]);

        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 新增配置
     */
    public function add(){
        if($this->request->isPost()){
            $data = $this->request->post();
            //验证
			$result = $this->validate($data,[
                'name|标识' => 'require|unique:config',
                'title|名称' => 'require'
            ]);
			// 验证失败 输出错误信息
            if(true !== $result) $this->error($result);
            
            if($query = ConfigModel::create($data)){
				cache('db_config_data',null);
				$this->success('新增成功');
			}else{
				$this->error('新增失败');
			}
        }else{
            $this->assign('info',null);
    		return $this->fetch('edit');
        }
    }

    /**
     * 编辑配置
     */
    public function edit($id = 0){
        if($this->request->isPost()){
            $data = $this->request->post();
            //验证
			$result = $this->validate($data,[
                'name|标识' => 'require|unique:config',
                'title|名称' => 'require'
            ]);
			// 验证失败 输出错误信息
            if(true !== $result) $this->error($result);
            
            if($query = ConfigModel::update($data)){
				cache('db_config_data',null);
				$this->success('更新成功');
			}else{
				$this->error('更新失败');
			}
        }else{
            $info = ConfigModel::where('id', $id)->find();
            if(false === $info){
                $this->error('获取配置信息错误');
            }
            $this->assign('info',$info);
            return $this->fetch();
        }
    }

    /**
     * 删除配置
     */
    public function del($id = 0){
        empty($id) && $this->error('请选择要操作的数据');

        if(is_array($id)){
            $map['id'] = ['in', $id];
        }elseif(is_numeric($id)){
            $map['id'] = $id;
        }

        if($query = ConfigModel::where($map)->delete()){
            cache('db_config_data',null);
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * 更新数据
     */
    public function update(){
        $data = $this->request->param();

        if(empty($data)){
            $this->error('请选择操作数据');
        }

        if($query = ConfigModel::update($data)){
            $this->success('更新成功');
        }else{
            $this->error('更新失败');
        }
    }

    /**
     * 获取某个标签的配置参数
     */
    public function group(){
        $group = config('app.config_group_list'); 

        $list = ConfigModel::where('status',1)->field('id,name,title,extra,value,remark,type,group')->order('sort')->select()->toArray();

        $new_list = [];
        $field_type = ['input','input','textarea','textarea','select'];
        foreach ($list as $key => $value){
            $value['type'] = $field_type[$value['type']];
            if($value['group'] == 0){
                $new_list[1][] = $value;
            }else{
                $new_list[$value['group']][] = $value;
            }
        }
        $this->assign('group', $group);
        $this->assign('list', $new_list);
        return $this->fetch();
    }

    /**
     * 批量保存配置
     */
    public function save(){
        $config = $this->request->post();
        if($config && is_array($config)){
            foreach ($config as $name => $value) {
                $map = array('name' => $name);
                ConfigModel::where($map)->setField('value', $value);
            }
            cache('db_config_data',null);
            $this->success('保存成功');
        }
    }    
}